1 00:00:00,400 --> 00:00:04,530 Now let's actually save the data from the leaderboard to the data store. 2 00:00:04,540 --> 00:00:09,650 We initialized it, but we haven't saved anything that we've obtained in the game. 3 00:00:09,670 --> 00:00:17,300 Let's go down below initialized player data and above our player added. 4 00:00:17,330 --> 00:00:19,000 Want to add another function? 5 00:00:20,260 --> 00:00:28,780 Local function save player data and then we will have the player get passed in. 6 00:00:28,900 --> 00:00:33,910 I'm going to get a data, a data variable. 7 00:00:33,910 --> 00:00:37,090 Just initialize it to an empty table right now. 8 00:00:37,180 --> 00:00:50,260 And then I'm going to get my data points from player leader stats, dot points, dot value. 9 00:00:50,620 --> 00:00:52,600 I'm going to do three of these. 10 00:00:52,600 --> 00:00:53,380 Write control. 11 00:00:53,380 --> 00:00:59,680 C control V control V one is going to be kills, one is going to be Dats doesn't have doesn't matter 12 00:00:59,680 --> 00:01:03,220 what order kills, but doesn't matter about spelling. 13 00:01:03,220 --> 00:01:04,780 So make sure you spell these right. 14 00:01:05,110 --> 00:01:06,910 And kills here. 15 00:01:08,730 --> 00:01:10,020 Deaths. 16 00:01:12,210 --> 00:01:12,900 Oh, my goodness. 17 00:01:12,900 --> 00:01:13,560 There we go. 18 00:01:13,590 --> 00:01:14,730 Deaths. 19 00:01:17,730 --> 00:01:19,070 Deaths there. 20 00:01:19,110 --> 00:01:31,400 And then we're going to do our DS set async for our player user ID and just pass in the data. 21 00:01:31,410 --> 00:01:36,300 So we're going to get all the stuff from the leader stats and then put it in this data structure, this 22 00:01:36,300 --> 00:01:39,510 data table right here and then save it off. 23 00:01:39,510 --> 00:01:48,660 But we also want to save things intermittently as the game runs just in case something weird happens. 24 00:01:48,930 --> 00:01:57,630 Let's do this local function save loop and I'm going to save the data for all the players. 25 00:01:57,630 --> 00:01:59,010 Periodically. 26 00:01:59,970 --> 00:02:06,030 I'll do a while and I'm going to do it for 5 seconds because this is a demo you might actually want 27 00:02:06,030 --> 00:02:08,310 to make it like every 30 seconds or something. 28 00:02:08,970 --> 00:02:16,920 Do so while wait five 5 seconds do local players is going to be my player service. 29 00:02:16,920 --> 00:02:21,210 I'm going to do a game dot players, colon, get children. 30 00:02:21,210 --> 00:02:22,980 It's going to get all the players in the game. 31 00:02:22,980 --> 00:02:35,970 They'll do a for loop for I and player in pairs from our players right that's the group of players do 32 00:02:36,600 --> 00:02:44,580 we'll get our board let's check to see if there's a board because there might not be right We start 33 00:02:44,580 --> 00:02:48,840 initializing players and doing things prior to the board being added. 34 00:02:48,840 --> 00:02:53,850 This is going to fire for everybody in the game, not just the player being added. 35 00:02:53,850 --> 00:02:56,700 So it's possible you get a player that doesn't have a board. 36 00:02:56,700 --> 00:03:01,740 Let's check to make sure they have a board because we're using it for saving stuff. 37 00:03:01,740 --> 00:03:05,000 So we'll just say leader stats, right? 38 00:03:05,010 --> 00:03:09,180 We're going to get the player, we're going to do a fine first child for leader stats, make sure they 39 00:03:09,180 --> 00:03:10,140 have the board. 40 00:03:11,070 --> 00:03:18,450 If board, then we'll call this save player data for that player 41 00:03:21,000 --> 00:03:22,860 and we can print two, right? 42 00:03:22,860 --> 00:03:32,730 So we do a print print in save loop in save loop for player and you'll want to get rid of this because 43 00:03:32,730 --> 00:03:35,370 if you have like 50 players is going to loop for everybody. 44 00:03:35,580 --> 00:03:38,640 Player dot name. 45 00:03:38,640 --> 00:03:40,110 It'll be good for testing though. 46 00:03:40,350 --> 00:03:41,100 You know what I'm going to do? 47 00:03:41,100 --> 00:03:45,570 I'm just going to put a little bit weight in between there because we are making calls to the data store, 48 00:03:45,600 --> 00:03:47,970 maybe slow down the network traffic a little bit. 49 00:03:48,660 --> 00:03:48,930 All right. 50 00:03:48,930 --> 00:03:53,130 So where do we call this and where do we call this? 51 00:03:53,220 --> 00:03:58,590 Well, this one we definitely want to call it when there's a player being removed from the game. 52 00:03:59,010 --> 00:03:59,400 Right. 53 00:03:59,400 --> 00:04:00,960 So let's copy that. 54 00:04:01,680 --> 00:04:03,150 So we keep it in mind. 55 00:04:03,400 --> 00:04:04,500 I'm going to do this. 56 00:04:04,500 --> 00:04:05,970 I could do this below. 57 00:04:05,970 --> 00:04:07,050 Player added. 58 00:04:07,050 --> 00:04:08,160 I think I will. 59 00:04:08,280 --> 00:04:17,250 Let's go down to the bottom, the very bottom of the script game players player removing. 60 00:04:17,250 --> 00:04:20,340 So that's when a player is being yanked from the game or one leaving the game. 61 00:04:20,340 --> 00:04:28,170 Right When they're leaving the game, connect save player data and the player will get passed in as 62 00:04:28,170 --> 00:04:30,930 an argument because it's much like Player added. 63 00:04:30,930 --> 00:04:36,930 So the player that's being removed is going to have the variable in sent to save player data. 64 00:04:37,200 --> 00:04:43,920 Now, one thing to keep in mind when you're leaving Roadblock Studio, this might not fire fast enough 65 00:04:43,920 --> 00:04:48,270 because Roadblock Studio shuts down right away, so the player removing will be fired. 66 00:04:48,270 --> 00:04:52,470 You might not save data when testing if that's really important to you. 67 00:04:52,470 --> 00:04:56,580 You should look up something called bind to close. 68 00:04:58,740 --> 00:05:04,710 Bind to close fires when the server is shutting down so that you could make sure you get one last save 69 00:05:04,710 --> 00:05:05,370 in there. 70 00:05:05,370 --> 00:05:12,390 And it's good for studio testing, but in actuality you're really only going to have to watch the player 71 00:05:12,390 --> 00:05:13,350 removing event. 72 00:05:13,350 --> 00:05:18,480 You're just going to have to be careful when you're doing your test in studio that this might not save 73 00:05:18,480 --> 00:05:19,380 everything. 74 00:05:19,740 --> 00:05:20,220 All right. 75 00:05:20,220 --> 00:05:25,830 So we have that firing when somebody leaves, we need to get this going. 76 00:05:26,490 --> 00:05:31,500 So I think what I'm going to do is copy, maybe do a control C for that. 77 00:05:31,860 --> 00:05:36,180 And we only want that to start one time. 78 00:05:37,020 --> 00:05:43,470 Like not every single time a player enters, maybe just on server startup. 79 00:05:43,470 --> 00:05:53,430 So let's do a spawn save loop and this is going to spawn off of a thread for the save loop and it should 80 00:05:53,430 --> 00:05:57,390 only happen on server startup when people join our game. 81 00:05:57,390 --> 00:06:00,030 This is going to happen every time someone joins the game. 82 00:06:00,030 --> 00:06:01,200 We don't need that many. 83 00:06:01,200 --> 00:06:07,560 We don't need that many calls to our our save loop because it is already calling all of our players. 84 00:06:07,770 --> 00:06:14,640 Let's go ahead and give it a try, see if we can get some data going now and save it off in between 85 00:06:14,640 --> 00:06:16,950 our in between our games. 86 00:06:17,220 --> 00:06:19,680 We're going to get a couple of coins maybe, and then die. 87 00:06:19,710 --> 00:06:20,820 Maybe two. 88 00:06:21,570 --> 00:06:22,410 Let's see. 89 00:06:23,680 --> 00:06:26,320 We can't do kills because we only have one player. 90 00:06:26,320 --> 00:06:27,760 We can die, though. 91 00:06:27,970 --> 00:06:28,840 Let's try that. 92 00:06:33,140 --> 00:06:33,460 Bam. 93 00:06:33,590 --> 00:06:34,940 So we got a death. 94 00:06:36,930 --> 00:06:38,210 And we can get. 95 00:06:38,220 --> 00:06:38,700 What was that? 96 00:06:38,850 --> 00:06:39,820 That was my head. 97 00:06:39,840 --> 00:06:41,820 We can get a we can get a zombie. 98 00:06:41,820 --> 00:06:42,540 Two or a coin. 99 00:06:42,540 --> 00:06:43,560 Let's get a coin. 100 00:06:44,500 --> 00:06:45,730 We got our gun. 101 00:06:50,940 --> 00:06:52,650 So we got ten points there. 102 00:06:52,680 --> 00:06:54,390 20 points, maybe 30. 103 00:06:54,840 --> 00:06:57,910 So we got 30 points and we got one death. 104 00:06:57,930 --> 00:07:00,580 Let's take a look at our output window. 105 00:07:00,600 --> 00:07:06,030 Remember, we may not be able to save as we're leaving because the player removing event doesn't fire 106 00:07:06,030 --> 00:07:08,250 fast enough for the studio to shut down. 107 00:07:08,250 --> 00:07:10,860 It will for regular for regular servers in your game. 108 00:07:10,860 --> 00:07:16,020 But we're getting all kinds of these calls that the data store data store request was added to the queue. 109 00:07:16,110 --> 00:07:16,560 All right. 110 00:07:16,560 --> 00:07:18,360 Let's go ahead and shut down. 111 00:07:22,600 --> 00:07:23,620 We're all shut down. 112 00:07:23,620 --> 00:07:24,910 Let's go ahead and play. 113 00:07:30,900 --> 00:07:32,150 And there we go. 114 00:07:32,160 --> 00:07:33,300 30 points. 115 00:07:33,300 --> 00:07:34,350 One death. 116 00:07:34,350 --> 00:07:35,640 That's looking good. 117 00:07:36,030 --> 00:07:38,460 Now, there's a couple of things you need to keep in mind. 118 00:07:38,460 --> 00:07:44,420 We tested our data store in Roadblock studio for our player. 119 00:07:44,430 --> 00:07:51,660 If you play that player in the game in roadblocks on a legit roadblocks player in a regular game, that 120 00:07:51,660 --> 00:07:53,340 date is still going to be there. 121 00:07:53,340 --> 00:07:59,360 The data store is not test test data store data, it's the actual data store. 122 00:07:59,370 --> 00:08:05,340 So if you mess up your data in Roadblock studio, you're going to mess up your data in the game. 123 00:08:05,670 --> 00:08:12,150 Now, the test server data, although it is the same data store, same as the production game, your 124 00:08:12,150 --> 00:08:13,830 user IDs are different. 125 00:08:13,830 --> 00:08:16,260 They usually make it like negative one and negative two. 126 00:08:16,260 --> 00:08:18,390 I don't know if they change that anytime soon. 127 00:08:18,390 --> 00:08:25,290 Probably they change a lot of stuff in roadblocks, but if we test under the test server, we're going 128 00:08:25,290 --> 00:08:31,800 to not we're not going to be able to see the results if we play in regular studio, not with the test 129 00:08:31,800 --> 00:08:33,960 server or in the game. 130 00:08:33,960 --> 00:08:36,870 And I'm going to demonstrate because that's really hard to explain. 131 00:08:36,870 --> 00:08:38,090 I'm going to pause the video. 132 00:08:38,090 --> 00:08:39,330 All this is starting up. 133 00:08:40,290 --> 00:08:40,620 All right. 134 00:08:40,620 --> 00:08:44,100 So I got him down to almost dead. 135 00:08:45,620 --> 00:08:49,140 And there we go. 136 00:08:49,160 --> 00:08:50,930 So we got to kill. 137 00:08:51,020 --> 00:08:54,220 And other player got a death. 138 00:08:54,230 --> 00:08:56,090 Let's shut this down. 139 00:08:56,090 --> 00:08:57,110 Bring it up. 140 00:08:57,110 --> 00:09:00,060 See if we maintained our leaderboard. 141 00:09:00,080 --> 00:09:01,490 I'll pause the video again. 142 00:09:01,490 --> 00:09:02,210 Shut it down. 143 00:09:02,210 --> 00:09:02,810 Bring it up. 144 00:09:02,810 --> 00:09:05,270 You can see I'm going to do clean up right here. 145 00:09:07,190 --> 00:09:08,480 And we got it right. 146 00:09:08,480 --> 00:09:11,610 We got our one kills and one death. 147 00:09:11,630 --> 00:09:13,640 Now he's at 50 points. 148 00:09:13,640 --> 00:09:15,600 One kill, one death. 149 00:09:15,620 --> 00:09:23,830 What happens if we close this all out and start up our regular or regular test server on Roadblock studio? 150 00:09:23,840 --> 00:09:26,570 Not the under the test tab. 151 00:09:26,570 --> 00:09:30,050 So if we go here, try this out. 152 00:09:33,150 --> 00:09:37,740 We have our original 30 points one deaths with no kills. 153 00:09:37,740 --> 00:09:43,380 So you can see that they use different IDs on this test tab for this server right here. 154 00:09:43,380 --> 00:09:45,750 So that actually is considered the test server. 155 00:09:45,750 --> 00:09:48,780 This isn't the test server, this is roadblock studio testing. 156 00:09:49,380 --> 00:09:56,160 So keep that in mind When you start doing the test server with a different players, your user IDs are 157 00:09:56,160 --> 00:09:59,040 going to be like negative one, negative two, negative three. 158 00:09:59,040 --> 00:10:06,090 And this is the actual player user ID when I just hit the test right here, the play button right here 159 00:10:06,090 --> 00:10:07,470 in Roadblock Studio.